package io.xxx.xbutler.core.get;

import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.client.PromotionClient;
import io.xxx.xbutler.domain.Jigsaw;
import io.xxx.xbutler.domain.RoomCategory;
import io.xxx.xbutler.domain.task.PushContext;
import io.xxx.xbutler.domain.task.Task;
import io.xxx.xbutler.domain.task.TaskItem;
import io.xxx.xbutler.domain.task.TaskScene;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 手动指定门店的商品卡片替换
 */
@Slf4j
@Component
public class CustomStoreProductGetter implements ContentGetter {

    @Resource
    private PromotionClient promotionClient;

    @Override
    public void replaceContent(PushContext context) {
        Task task = context.getTask();
        Long storeId = task.getStoreId();
        if (task.getScene() == TaskScene.Push || storeId == null) {
            return;
        }

        RoomCategory roomCategory = task.getRoomCategory();
        List<TaskItem> taskItems = context.getTaskItems();
        for (TaskItem taskItem : taskItems) {
            if (!taskItem.isProduct()) {
                continue;
            }
            if (roomCategory != RoomCategory.MaterialRoom && roomCategory != RoomCategory.CustomerServiceRoom) {
                continue;
            }

            JSONObject content = taskItem.getContent();
            String skuSn = content.getString("skuSn");
            Jigsaw productJigsaw = promotionClient.getProductJigsaw(storeId, skuSn);
            content.put("thumbUrl", productJigsaw.getThumbUrl());
            taskItem.setContent(content);
        }
    }
}
